﻿using System;
using Google.GData.Calendar;
using Google.GData.Client;
using Google.GData.Extensions;

namespace Byteabyte
{
    class GCalClient
    {
        /// <summary>
        /// Crea un evento en el calendario de Google a partir de los datos leidos del fichero ICS
        /// </summary>
        /// <param name="icsFile">Parsed ics file</param>
        public static void CreateEvents(IcsFile icsFile, Config config)
        {
            var service = new CalendarService("GCalICSImporter");

            service.setUserCredentials(config.UserName, config.Password);

            foreach (var icsEvent in icsFile.Events)
            {
                var entry = new EventEntry();

                entry.Title.Text = icsEvent.Summary;
                entry.Content.Content = icsEvent.Description;

                // Set a location for the event.
                //Where eventLocation = new Where();
                //eventLocation.ValueString = "South Tennis Courts";
                //entry.Locations.Add(eventLocation);

                var eventTime = new When(icsEvent.DtStart, icsEvent.DtEnd);
                entry.Times.Add(eventTime);

                var postUri = new Uri(GCalStrings.CalendarUri);
                AtomEntry insertedEntry = service.Insert(postUri, entry);
            }
        }
    }
}
